import token from './Token.js';
  const {getstorage}=token;
const DEFAULT_CONGIG = {
   // baseUrl:"http://192.168.137.1:8099",
  baseUrl:"",
  data: {},
    header: {
      'Content-Type': 'application/json', // 设置 Content-Type 请求头
      'Authorization': getstorage() // 设置 Authorization 请求头
    },
    method: "post",
};
class Request{
  constructor (options={}){
    
  }
  //请求拦截
  requestInterceptor(url, data, config, method){
    url = DEFAULT_CONGIG.baseUrl + url;
    const header=DEFAULT_CONGIG.header;
    const configs ={
      ...this.config,
      url,
      data,
      ...config,
      method,
      header
    };
    return configs
  }
  async responseInterceptor(res) {
      const { data: _data } = res;
      const { code, message, data } = _data;
      if (code != "200") {
        console.log(res)
        uni.navigateTo({
          url:"/pages/login/login"
        })
        return Promise.reject(message);
      }
      return _data;
    }
    request(url,data,config,method){
      uni.showLoading();
      const _config = this.requestInterceptor(url,data,config,method); 
      return new Promise((resolve,reject) => {
        uni.request({
          ..._config,
          sslVerify:  false,
          success: (res) => {
            this.responseInterceptor(res).then(resolve).catch(reject);
          },
          fail: (err) => {
            console.log("fail",err);
          },
          complete() {
            uni.hideLoading();
          },
        });
      });
    }
  get(url,data={},config={}) {
    return this.request(url,data,config,"GET");
  }
  post(url, data = {}, config = {}) {
      return this.request(url, data, config, "POST");
  }
}
export default Request;